﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using Helpers;

namespace SecurityManager
{
    public class Authorization : IAuthorization
    {
        private string _token;
        private string _imei;
        private string _phoneno;
        private string _hash;
        private bool _isValid;

        public Authorization(string token)
        {
            _token = token;
            ExplodeToken();
            ValidateToken();
        }

        public string GetIMEI()
        {
            return _isValid ? _imei : null;
        }

        public string GetPhoneNo()
        {
            return _isValid ? _phoneno : null;
        }

        private bool ValidateToken()
        {
            _isValid = HashCalc.verifyMd5Hash(string.Format("{0}|{1}", _imei, _phoneno), _hash);
            return _isValid;
        }

        private void ExplodeToken()
        {
            string[] tokenSplit = _token.Split('|');
            _imei = tokenSplit[0];
            _phoneno = tokenSplit[1];
            _hash = tokenSplit[2];
        }

    }
}
